<?php
/**
 * +--------------------------------------------------------------
 * | eLuoPHP
 * +--------------------------------------------------------------
 * | Copyright (c) 2013-2019 http://qxwoo.cn All rights reserved.
 * +--------------------------------------------------------------
 * | y139w <y139w@163.com>
 * +--------------------------------------------------------------
 * | 2019/08/05 21:07:57
 * +--------------------------------------------------------------
 * | Pretty.php
 * +--------------------------------------------------------------
 */


namespace luo\traits;


trait Pretty
{

    private static $words = [
        'namespace'   => '<span class="namespace">namespace</span>',
        'private'     => '<span class="private">private</span>',
        'protected'   => '<span class="protected">protected</span>',
        'public'      => '<span class="public">public</span>',
        'class'       => '<span class="class">class</span>',
        'function'    => '<span class="function">function</span>',
        'return'      => '<span class="return">return</span>',
        'static'      => '<span class="static">static</span>',
    ];

    public static function handle($content){

        $content = str_replace('&lt;?php', '<span class="tag">&lt;?php</span>', $content);
        foreach(self::$words as $key=>$val){
            $content = preg_replace("/{$key}\s/s", "{$val} ", $content);
        }
        // $content = self::symbol($content);
        $content = self::control($content);
        $content = self::variable($content);
        $content = self::method($content);
        $content = self::property($content);
        $content = self::comment($content);
        $content = self::func($content);
        return $content;

    }

    private static function symbol($content){

        $symbol = [
            '\+' => '<span class="symbol">+</span>',
            '\-' => '<span class="symbol">-</span>',
            '\[' => '<span class="symbol">[</span>',
            '\]' => '<span class="symbol">]</span>',
            '\(' => '<span class="symbol">(</span>',
            '\)' => '<span class="symbol">)</span>',
            '\{' => '<span class="symbol">{</span>',
            '\}' => '<span class="symbol">}</span>',
            '\:' => '<span class="symbol">:</span>',
            '\?' => '<span class="symbol">?</span>',
            //'\;' => '<span class="symbol">;</span>',
        ];
        foreach($symbol as $key=>$val){
            $content = preg_replace("/{$key}/", "{$val}", $content);
        }
        return $content;

    }

    private static function control($content){

        $words = [
            'foreach'     => '<span class="foreach">foreach</span>',
            'if'          => '<span class="if">if</span>',
            'while'       => '<span class="while">while</span>',
            'for\('       => '<span class="for">for</span>(',
            'true'        => '<span class="true">true</span>',
            'false'       => '<span class="false">false</span>',
            'null'        => '<span class="null">null</span>',
            'else'        => '<span class="else">else</span>',
        ];
        foreach($words as $key=>$val){
            $content = preg_replace("/{$key}/s", "{$val}", $content);
        }
        return $content;

    }

    private static function func($content){

        preg_match_all('/(.*?)\(/', $content, $array);
        //dump($array);
        return $content;

    }

    private static function variable($content){

        $content = str_replace('$', '&sect;', $content);

        preg_match_all("/&sect;[a-zA-Z_0-9]+\w/s", $content, $array);
        foreach($array[0] as $val){
            $content = str_replace($val, '<span class="variable">' . $val . '</span>', $content);
        }
        $content = str_replace('&sect;', '$', $content);

        return $content;

    }

    private static function property($content){

        preg_match_all('/&gt;([a-zA-Z0-9_]+\w)/', $content, $array);
        foreach($array[1] as $val){
            $content = str_replace("-&gt;{$val}", '-&gt;<span class="property">' . $val . '</span>', $content);
        }
        return $content;

    }

    private static function method($content){

        preg_match_all('/&gt;([a-zA-Z0-9_]+\w)\(/s', $content, $array);
        foreach($array[1] as $val){
            $content = str_replace("-&gt;{$val}(", '-&gt;<span class="method">' . $val . '</span>(', $content);
        }
        return $content;

    }

    private static function comment($content){

        preg_match_all('/\/\/.*?\n/s', $content, $array);
        foreach($array[0] as $val){
            $content = str_replace($val, '<span class="single-line-comment">' . $val . '</span>', $content);
        }
        preg_match_all('/\/\*(.*?)\*\//s', $content, $array);
        foreach($array[0] as $val){
            $content = str_replace($val, '<span class="comment">' . $val . '</span>', $content);
        }
        return $content;

    }

}